package org.xinjiang.data.migration.xinjiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.data.migration.common.response.PageInfo;
import org.xinjiang.data.migration.common.response.Param;
import org.xinjiang.data.migration.common.utils.query.Pages;
import org.xinjiang.data.migration.common.utils.query.Queries;
import org.xinjiang.data.migration.xinjiang.convert.XjJzdxConvert;
import org.xinjiang.data.migration.xinjiang.dto.req.XjJzdxQueryDTO;
import org.xinjiang.data.migration.xinjiang.dto.res.XjJzdxResultDTO;
import org.xinjiang.data.migration.xinjiang.dto.res.XjSubJzdxResultDTO;
import org.xinjiang.data.migration.xinjiang.entity.XjJzdxEntity;
import org.xinjiang.data.migration.xinjiang.mapper.XjJzdxMapper;
import org.xinjiang.data.migration.xinjiang.service.IXjJzdxService;

import java.util.List;

@Service
public class XjJzdxServiceImpl extends ServiceImpl<XjJzdxMapper, XjJzdxEntity> implements IXjJzdxService {
    @Autowired
    private XjJzdxConvert xjJzdxConvert;

    @Override
    public PageInfo<XjJzdxResultDTO> pageList(Param pageable, XjJzdxQueryDTO dto) {
        IPage<XjJzdxEntity> page = this.page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, xjJzdxConvert::convert);
    }

    @Override
    public IPage<XjSubJzdxResultDTO> findPageList(Param pageable, XjJzdxQueryDTO query) {
        IPage<XjSubJzdxResultDTO> pageList = this.baseMapper.findPageList(Pages.page(pageable), query);
        if (CollectionUtils.isNotEmpty(pageList.getRecords())) {
            this.postAddAttribute(pageList.getRecords());
        }
        return pageList;
    }

    /**
     * 数据经过了sql一次处理，复杂灵活的业务放到程序中进行二次后处理
     *
     * @param recordList 矫正对象及其属性列表
     */
    private void postAddAttribute(List<XjSubJzdxResultDTO> recordList) {
        if (CollectionUtils.isEmpty(recordList)) {
            return;
        }

        for (XjSubJzdxResultDTO record : recordList) {
            xjJzdxConvert.postAddAttribute(record);
        }
    }

    private LambdaQueryWrapper<XjJzdxEntity> wrapper(XjJzdxQueryDTO query) {
        LambdaQueryWrapper<XjJzdxEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(XjJzdxEntity::getId, v));
        Queries.accept(query.getZt(), v -> wrapper.eq(XjJzdxEntity::getZaicezhuangtai, v));
        Queries.accept(query.getSqjzrybh(), v -> wrapper.eq(XjJzdxEntity::getSqjzrybh, v));
        return wrapper;
    }
}
